/**
 * @ Author: luoqi
 * @ Create Time: 2024-08-01 15:37
 * @ Modified by: luoqi
 * @ Modified time: 2025-02-14 18:52
 * @ Description: 优化后的日志库
 */

#ifndef _QLOG_H
#define _QLOG_H

#include <bsp.h>

#ifndef QLOG
#define QLOG(fmt, ...)   bsp_printf(fmt, ##__VA_ARGS__)
#endif

#define QLOG_LVL_NONE             (0)
#define QLOG_LVL_ERR              (1)
#define QLOG_LVL_WARN             (2)
#define QLOG_LVL_DBG              (3)
#define QLOG_LVL_INFO             (4)

#ifndef QLOG_LVL
#define QLOG_LVL 0
#endif

#ifndef QLOG_TAG
#define QLOG_TAG "QLOG"
#endif

#define QLOG_TXT_COLOR_RESET    "\x1b[0m"
#define QLOG_TXT_COLOR_BLACK    "\x1b[30m"
#define QLOG_TXT_COLOR_RED      "\x1b[91m"
#define QLOG_TXT_COLOR_GREEN    "\x1b[92m"
#define QLOG_TXT_COLOR_YELLOW   "\x1b[93m"
#define QLOG_TXT_COLOR_BLUE     "\x1b[94m"
#define QLOG_TXT_COLOR_PURPLE   "\x1b[95m"
#define QLOG_TXT_COLOR_CYAN     "\x1b[96m"
#define QLOG_TXT_COLOR_WHITE    "\x1b[37m"

 /*
   通用日志辅助宏，增加了行号信息，后续可根据需要添加更多信息（如文件名、时间戳等）
 */
#define QLOG_HELPER(COLOR, LEVEL_STR, LEVEL, FMT, ...)      \
    do {                                                    \
        if (QLOG_LVL >= (LEVEL)) {                          \
            QLOG("%s\r\x1b[K " LEVEL_STR "(%s, %d): ",      \
                       COLOR, QLOG_TAG, __LINE__);          \
            QLOG(QLOG_TXT_COLOR_RESET);                     \
            QLOG(FMT, ##__VA_ARGS__);                       \
            QLOG("\r\n");                                   \
        }                                                   \
    } while (0)

#define QLOG_INFO(...) QLOG_HELPER(QLOG_TXT_COLOR_GREEN, "I", QLOG_LVL_INFO, __VA_ARGS__)
#define QLOG_DBG(...)  QLOG_HELPER(QLOG_TXT_COLOR_BLUE,  "D", QLOG_LVL_DBG,  __VA_ARGS__)
#define QLOG_WARN(...) QLOG_HELPER(QLOG_TXT_COLOR_YELLOW,"W", QLOG_LVL_WARN, __VA_ARGS__)
#define QLOG_ERR(...)  QLOG_HELPER(QLOG_TXT_COLOR_RED,   "E", QLOG_LVL_ERR,  __VA_ARGS__)

#endif
